//
//  ZZBPopView.m
//  recordworktool_ios
//
//  Created by mc on 2017/12/7.
//  Copyright © 2017年 ZhangZhanbin. All rights reserved.
//

#import "ZZBPopView.h"

@implementation ZZBPopView

#pragma mark - init
- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupDefault];
    }
    return self;
}

- (void)setupDefault {
    
    // 设置自身的属性
    self.bounds = [UIScreen mainScreen].bounds;
    
    // 背景半透明视图
    self.maskView = [UIButton buttonWithType:UIButtonTypeCustom];
    _maskView.frame = self.bounds;
    _maskView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    _maskView.alpha = 0.0;
    [self addSubview:_maskView];
    [_maskView addTarget:self action:@selector(remove) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - tap
- (void)show {
    
    // 把本视图添加到窗口上
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self setCenter:[UIApplication sharedApplication].keyWindow.center];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
    
    // 显示动画
    [self contentViewShowAnimation];
}
#pragma mark - animation
- (void)contentViewShowAnimation {
    
    _maskView.alpha = 0;
    
    UIButton *topBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_maskView addSubview:topBtn];
    [topBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_maskView.mas_centerY).offset(-30);
        make.centerX.equalTo(_maskView.mas_centerX).offset(0);
        make.size.mas_equalTo(CGSizeMake(120, 120));
    }];
    topBtn.backgroundColor = BY_THEME_COLOR;
    topBtn.layer.cornerRadius = 60;
    [topBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    topBtn.tag = 100;
    topBtn.alpha = 0;
    [topBtn setTitle:@"工时明细" forState:UIControlStateNormal];
    [topBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.topBtn = topBtn;
    UIButton *bottomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_maskView addSubview:bottomBtn];
    [bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_maskView.mas_centerY).offset(30);
        make.centerX.equalTo(_maskView.mas_centerX).offset(0);
        make.size.mas_equalTo(CGSizeMake(120, 120));
    }];
    bottomBtn.alpha = 0;
    bottomBtn.backgroundColor = BY_THEME_COLOR;
    bottomBtn.backgroundColor = BY_THEME_COLOR;
    bottomBtn.layer.cornerRadius = 60;
    [bottomBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    bottomBtn.tag = 200;
    [bottomBtn setTitle:@"工事明细" forState:UIControlStateNormal];
    [bottomBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.bottomBtn = bottomBtn;
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        _maskView.alpha = 1;
        
        topBtn.alpha = 1;
        bottomBtn.alpha = 1;
        
        
       
    } completion:^(BOOL finished){
        
    }];
}
- (void)remove {
    
    // 隐藏动画
    [self contentViewRemoveAnimation];
    
    
}

- (void)contentViewRemoveAnimation {
    
  
    _maskView.alpha = 1;
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        
        _maskView.alpha = 0;
        
       
    } completion:^(BOOL finished){
        
        [self removeFromSuperview];
    }];
}


- (void)btnAction:(UIButton *)btn{
    
    [self remove];
    [self performSelector:@selector(perform1111:) withObject:[NSNumber numberWithInteger:btn.tag] afterDelay:0.5];
    
}

- (void)perform1111:(id)value{
    NSNumber *number = value;
    self.indexAtView(number.integerValue);
}


@end
